package com.salinity.kun.task;

import java.io.IOException;
import java.text.ParseException;
import java.util.List;

import com.mathworks.toolbox.javabuilder.MWException;
import com.salinity.kun.algorithm.harmonic.GaussianElimination;
import com.salinity.kun.algorithm.harmonic.PCA;
import com.salinity.kun.helper.MatrixHelper;

import Jama.Matrix;
import PCR.PCR;

/**
 * 来源：https://blog.csdn.net/XiaoXiao_Yang77/article/details/79278398
 * 
 * @author Administrator
 *
 */
public class PCATask implements ITask {

	private double[][] primaryArray;
	private double [][] fb;

	public PCATask(double[][] primaryArray,double[][] fb) {
		this.primaryArray = primaryArray;
		this.fb = fb;
	}

	@Override
	public boolean run() throws IOException, ParseException {

	
		
		PCA pca = new PCA();
		// 获得样本集
		System.out.println("--------------------------------------------");
		Matrix m = new Matrix(primaryArray);
		m.print(4, 6);
		
		
		double[][] averageArray = pca.changeAverageToZero(primaryArray);
		System.out.println("--------------------------------------------");
		System.out.println("均值0化后的数据: ");
		System.out.println(averageArray.length + "行，" + averageArray[0].length + "列");
		MatrixHelper.printMatrix(averageArray);
		System.out.println("---------------------------------------------");
		System.out.println("协方差矩阵: ");
		double[][] varMatrix = pca.getVarianceMatrix(averageArray);
		MatrixHelper.printMatrix(varMatrix);
		System.out.println("--------------------------------------------");
		System.out.println("特征值矩阵: ");
		double[][] eigenvalueMatrix = pca.getEigenvalueMatrix(varMatrix);
		MatrixHelper.printMatrix(eigenvalueMatrix);

		System.out.println("--------------------------------------------");
		System.out.println("特征向量矩阵: ");
		double[][] eigenVectorMatrix = pca.getEigenVectorMatrix(varMatrix);
		MatrixHelper.printMatrix(eigenVectorMatrix);
		
		System.out.println("--------------------------------------------");
		
		List<Matrix> rsltList = pca.getPrincipalComponent_V2(primaryArray, eigenvalueMatrix, eigenVectorMatrix);
		Matrix eigenValueRslt = rsltList.get(0);
		Matrix principalMatrix = rsltList.get(1);
		System.out.println("主成分特征值矩阵与对应的特征向量：");
		eigenValueRslt.print(4, 6);
		principalMatrix.print(4, 6);
		
		System.out.println("ilamda * c^-1 b");
		new Matrix(eigenvalueMatrix).inverse().print(4, 6);
		pca.inverseDiagonalMatrix(new Matrix(eigenvalueMatrix)).print(4, 6);;
	
		Matrix CTB = pca.inverseDiagonalMatrix(eigenValueRslt).times(principalMatrix.inverse()).times(new Matrix(fb));
		Matrix rsltMatrix = CTB.transpose().times(principalMatrix.transpose());

//		System.out.println("--------------------------------------------");
//		System.out.println("降维后的矩阵: ");
//		Matrix resultMatrix = pca.getResult(primaryArray, principalMatrix);
//	     resultMatrix.print(6, 3);
//	     System.out.println("--------------------------------------------");
//	     System.out.println("求取回归模型的矩阵解: ");
	     
	  
	     
//	     
//	     double[][] a = resultMatrix.getArray();
//	     
//	     double[][] forRsltMatrix = new double[a.length][a[0].length+1];
//	     
//	     for(int i=0;i<a.length;i++) {
//	    	 for(int j=0;j<a[0].length;j++) {
//	    		 forRsltMatrix[i][j] = a[i][j];
//	    	 }
//	    	 forRsltMatrix[i][a[0].length]=fb[i][0];
//	     }
//	     
//	     new Matrix(a).solve(new Matrix(fb)).print(4, 6);
//	   
//		System.out.println(resultMatrix.getRowDimension() + "," + resultMatrix.getColumnDimension());
		return true;
	}
	
	public static void main(String[] args) {
		try {
			PCR pcr = new PCR();
			
			Object x[][] = {{1,2},{3,8},{2,5}};
			Object y[] = {1,4,2};
			
			pcr.doPCR(2, x,y);
		} catch (MWException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
